package mk.com.ldm.corepoint.administration.client.view;

import java.util.ArrayList;

import mk.com.ldm.corepoint.administration.client.presenter.EditPresenter;

import org.gwt.mosaic.ui.client.WindowPanel;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EditView extends Composite implements EditPresenter.Display{

	private final WindowPanel winModal;
	private FlexTable form;
	private final Button create;
	private final Button cancel;
	private Button addPage;
	private Button addEvent;
	private Button addForeign;
	private Button addUser;
	private Button addPermission;
	private Button setPageName;
	private Button saveAppSettings = new Button("Save");
	private Button save;
	private final TextBox name;
	private final TextBox pageName;
	private ArrayList<Widget> mainFormWidgets, eventFormWidgets, foreignFormWidgets, userFormWidgets, permissionsFormWidgets,
							rolePermissionsFormWidgets, appSettingsFormWidgets;
	private FlexTable widgetList;
	private FlexTable objectList;
	private Integer currentIndex = 0;
	private String description;
	
	public EditView(String type, String id){
		winModal = new WindowPanel();
		objectList = new FlexTable();
				
		create = new Button("Create");
		cancel = new Button("Cancel");
		save = new Button("Save");
		
		name = new TextBox();
		pageName = new TextBox();
		
		String firstLetter = type.substring(0,1); 
        String remainder   = type.substring(1);    
        type = firstLetter.toUpperCase() + remainder.toLowerCase();
        
        firstLetter = id.substring(0,1); 
        remainder   = id.substring(1);    
        id = firstLetter.toUpperCase() + remainder.toLowerCase();
		
        winModal.setCaption(type+" "+id);
        
		form = new FlexTable();
		
		generateForm(id);
		
		FlexTable layout = new FlexTable();
		
		layout.setWidget(0, 0, new Label(description));
		layout.setWidget(1, 0, form);
		layout.setWidget(2, 0, objectList);
		
		winModal.setWidget(layout);
		
		winModal.pack();
		winModal.center();
		winModal.show();
	}
		
	public void generateForm(String id){
		if(id.equals("Project")){
			generateProjectForm();
			description = "Create a new project";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			form.setWidget(1, 0, create);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Name")){
			generatePageNameForm();
			description = "Create a new page for the project";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			setPageName = new Button("Save");
			form.setWidget(1, 0, setPageName);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Page")){
			generatePageForm();
			description = "Add a new object to the page";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			addPage = new Button("Add");
			form.setWidget(1, 0, addPage);
			form.setWidget(1, 1, cancel);
			form.setWidget(2, 0, save);
		} else if(id.equals("Events")){
			generateEventForm();
			description = "Set events and handlers for this object";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			addEvent = new Button("Add");
			form.setWidget(1, 0, addEvent);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Foreign keys")){
			generateForeignForm();
			description = "Set foreign properties of this object";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			addForeign = new Button("Add");
			form.setWidget(1, 0, addForeign);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("User")){
			generateUserForm();
			description = "Create a new user";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			addUser = new Button("Add user");
			form.setWidget(1, 0, addUser);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Permission")) {
			generatePermissionsForm();
			description = "Customize permissions for this user";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			addPermission = new Button("Add permission");
			form.setWidget(1, 0, addPermission);
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Language")){
			generateLanguageForm();
		} else if(id.equals("Permissions")){
			generateRolesPermissionsForm();
			description = "Add or Edit permissions";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			form.setWidget(1, 0, new Button("Save"));
			form.setWidget(1, 1, cancel);
		} else if(id.equals("Application settings")){
			generateAppSettingsForm();
			description = "Edit Application Settings";
			form.getFlexCellFormatter().setColSpan(0, 0, 2);
			form.setWidget(1, 0, saveAppSettings);
			form.setWidget(1, 1, cancel);
		}
	}
	
	public void generateProjectForm(){
		Label nameLbl = new Label("Project name:");
		form.setWidth("250px");
		form.setWidget(0, 0, nameLbl);
		form.setWidget(0, 1, name);
	}
	
	public void generatePageNameForm(){
		Label nameLbl = new Label("Page name:");
		form.setWidget(0, 0, nameLbl);
		form.setWidget(0, 1, pageName);
	}
	
	public void generatePageForm(){
		
		widgetList = new FlexTable();
		mainFormWidgets = new ArrayList<Widget>();
			
		mainFormWidgets.add(new Label("Parameters:"));
		mainFormWidgets.add(new TextBox()); //ID
		mainFormWidgets.get(1).setTitle("parameters");
		mainFormWidgets.add(new Label("Form name:"));
		mainFormWidgets.add(new TextBox()); //Item name in Form
		mainFormWidgets.get(3).setTitle("formName");
		mainFormWidgets.add(new Label("Database name:"));
		mainFormWidgets.add(new TextBox()); //Item name in Database | Null if static
		mainFormWidgets.get(5).setTitle("dbName");
		
		ListBox formList = new ListBox();
		formList.addItem("Button");
		formList.addItem("Dropdown");
		formList.addItem("Checkbox");
		formList.addItem("Radiobutton");
		formList.addItem("Textbox");
		formList.addItem("Textarea");
		formList.addItem("Datepicker");
		
		mainFormWidgets.add(new Label("Widget type:"));
		mainFormWidgets.add(formList); //Widget type
		mainFormWidgets.get(7).setTitle("formType");
		mainFormWidgets.add(new Label("Widget size:"));
		mainFormWidgets.add(new TextBox()); //Widget size (radio button for example)
		mainFormWidgets.add(new Label("Enabled:"));
		mainFormWidgets.add(new CheckBox()); //If widget is enabled
		mainFormWidgets.get(11).setTitle("enabled");
		mainFormWidgets.add(new Label("Foreign:"));
		mainFormWidgets.add(new CheckBox()); //If widget is a foreign picker + add a picker button
		mainFormWidgets.get(13).setTitle("foreign");
		mainFormWidgets.add(new Label(""));
		mainFormWidgets.add(new Button("Set foreign relation")); //Foreign table/relation editor
		mainFormWidgets.add(new Label("Filter:"));
		mainFormWidgets.add(new CheckBox()); //If widget is a filter + criteria
		mainFormWidgets.get(17).setTitle("filter");
		mainFormWidgets.add(new Label("Default value:"));
		mainFormWidgets.add(new TextBox()); //Widget's default value
		mainFormWidgets.get(19).setTitle("value");
		
		ListBox dbList = new ListBox();
		dbList.addItem("serial");
		dbList.addItem("integer");
		dbList.addItem("character varying");
		dbList.addItem("varchar");
		dbList.addItem("date");
		dbList.addItem("boolean");
		
		mainFormWidgets.add(new Label("Database type:"));
		mainFormWidgets.add(dbList); //List of database types for the item
		mainFormWidgets.get(21).setTitle("dbType");
		mainFormWidgets.add(new Label("Database size:"));
		mainFormWidgets.add(new TextBox()); //Database type size | Null if unknown
		
		ListBox validationType = new ListBox();
		validationType.addItem("Text");
		validationType.addItem("Number");
		validationType.addItem("E-mail");
		validationType.addItem("Date");
		validationType.addItem("Boolean");
		
		mainFormWidgets.add(new Label("Validation type:"));
		mainFormWidgets.add(validationType); //Validation type for this field's expected input
		mainFormWidgets.get(25).setTitle("validationType");
		mainFormWidgets.add(new Label("Weight:"));
		mainFormWidgets.add(new TextBox());
		mainFormWidgets.get(27).setTitle("weight");
		mainFormWidgets.add(new Label("Visible:"));
		mainFormWidgets.add(new CheckBox()); //If widget is visible
		mainFormWidgets.get(29).setTitle("visible");
		mainFormWidgets.add(new Label("Column:"));
		mainFormWidgets.add(new TextBox()); //If widget should take a particular column
		mainFormWidgets.get(31).setTitle("col");
		mainFormWidgets.add(new Label("Row:"));
		mainFormWidgets.add(new TextBox()); //If widget should take a particular row
		mainFormWidgets.get(33).setTitle("row");
		
		mainFormWidgets.add(new Label("Events:"));
		mainFormWidgets.add(new Button("Set events")); //Widget event map
		
		mainFormWidgets.add(new Label("Include in simple:"));
		mainFormWidgets.add(new CheckBox());
		mainFormWidgets.add(new Label());
		mainFormWidgets.add(new Label());
		
		
		
		for(int i=0; i<mainFormWidgets.size(); i+=4){
			widgetList.setWidget(i, 0, mainFormWidgets.get(i));
			widgetList.setWidget(i, 1, mainFormWidgets.get(i+1));
			widgetList.setWidget(i, 2, mainFormWidgets.get(i+2));
			widgetList.setWidget(i, 3, mainFormWidgets.get(i+3));
		}
		
		form.setWidget(0, 0, widgetList);
	}
	
	public void generateEventForm(){
		widgetList = new FlexTable();
		eventFormWidgets = new ArrayList<Widget>();
		eventFormWidgets.add(new Label("Type:"));
		
		ListBox eventTypes = new ListBox();
		
		//Generated from Widget type / associated events
		eventTypes.addItem("onClick");
		eventTypes.addItem("onHover");
		eventTypes.addItem("onChange");
		eventTypes.addItem("onFocus");
		eventTypes.addItem("onBlur");
		
		eventFormWidgets.add(eventTypes); // The type of the event
		
		eventFormWidgets.add(new Label("Function fired:"));
		
		ListBox functionFired = new ListBox();
		
		//Read from library
		functionFired.addItem("testFunction1");
		functionFired.addItem("testFunction2");
		functionFired.addItem("testFunction3");
		functionFired.addItem("testFunction4");
		functionFired.addItem("testFunction5");
		
		eventFormWidgets.add(functionFired); // The function that should be fired on this event
		
		eventFormWidgets.get(0).setTitle("type");
		eventFormWidgets.get(2).setTitle("fires");
		widgetList.setWidget(0, 0, eventFormWidgets.get(0));
		widgetList.setWidget(0, 1, eventFormWidgets.get(1));
		widgetList.setWidget(1, 0, eventFormWidgets.get(2));
		widgetList.setWidget(1, 1, eventFormWidgets.get(3));
		
		form.setWidget(0, 0, widgetList);
		
	}
	
	public void generateForeignForm(){
		widgetList = new FlexTable();
		foreignFormWidgets = new ArrayList<Widget>();
		
		foreignFormWidgets.add(new Label("Table name:"));
		foreignFormWidgets.add(new TextBox()); // Foreign table name | Could be a dropdown
		foreignFormWidgets.add(new Label("Field name:")); // Foreign table field name | Could be a dropdown
		foreignFormWidgets.add(new TextBox());
		foreignFormWidgets.add(new Label("Id name:"));
		foreignFormWidgets.add(new TextBox());
		
		foreignFormWidgets.get(0).setTitle("tableName");
		foreignFormWidgets.get(2).setTitle("fieldName");
		widgetList.setWidget(0, 0, foreignFormWidgets.get(0));
		widgetList.setWidget(0, 1, foreignFormWidgets.get(1));
		widgetList.setWidget(1, 0, foreignFormWidgets.get(2));
		widgetList.setWidget(1, 1, foreignFormWidgets.get(3));
		widgetList.setWidget(2, 0, foreignFormWidgets.get(4));
		widgetList.setWidget(2, 1, foreignFormWidgets.get(5));
		
		form.setWidget(0, 0, widgetList);
	}
	
	public void generateUserForm(){
		widgetList = new FlexTable();
		userFormWidgets = new ArrayList<Widget>();
		
		userFormWidgets.add(new Label("Username:"));
		userFormWidgets.add(new TextBox());
		userFormWidgets.add(new Label("Password:"));
		userFormWidgets.add(new PasswordTextBox());
		userFormWidgets.add(new Label("Permissions role:"));
		
		ListBox permissionRoles = new ListBox();
		
		userFormWidgets.add(permissionRoles);
		userFormWidgets.add(new Label(""));
		userFormWidgets.add(new Button("Custom Permissions"));
		userFormWidgets.add(new Button("Add/Edit Roles"));
		
		widgetList.setWidget(0, 0, userFormWidgets.get(0));
		widgetList.setWidget(0, 1, userFormWidgets.get(1));
		widgetList.setWidget(1, 0, userFormWidgets.get(2));
		widgetList.setWidget(1, 1, userFormWidgets.get(3));
		widgetList.setWidget(2, 0, userFormWidgets.get(4));
		widgetList.setWidget(2, 1, userFormWidgets.get(5));
		widgetList.setWidget(3, 0, userFormWidgets.get(6));
		widgetList.setWidget(4, 1, userFormWidgets.get(8));
		widgetList.setWidget(4, 0, userFormWidgets.get(7));
		
		form.setWidget(0, 0, widgetList);
		
	}
	
	public void generatePermissionsForm(){
		widgetList = new FlexTable();
		permissionsFormWidgets = new ArrayList<Widget>();
		
		permissionsFormWidgets.add(new Label("Page:"));
		
		ListBox page = new ListBox();
		page.addItem("samplePage1");
		page.addItem("samplePage2");
		page.addItem("samplePage3");
		
		permissionsFormWidgets.add(page);
		permissionsFormWidgets.add(new Label("Select permission type:"));
		permissionsFormWidgets.add(new Label());
		permissionsFormWidgets.add(new RadioButton("permissionType", "Allowed"));
		permissionsFormWidgets.add(new RadioButton("permissionType", "Forbidden"));
		permissionsFormWidgets.add(new Label("Insert values (coma separated):"));
		permissionsFormWidgets.add(new Label());
		permissionsFormWidgets.add(new TextArea());
		permissionsFormWidgets.get(8).setWidth("100%");
		
		widgetList.setWidget(0, 0, permissionsFormWidgets.get(0));
		widgetList.setWidget(0, 1, permissionsFormWidgets.get(1));
		widgetList.setWidget(1, 0, permissionsFormWidgets.get(2));
		widgetList.setWidget(1, 1, permissionsFormWidgets.get(3));
		widgetList.setWidget(2, 0, permissionsFormWidgets.get(4));
		widgetList.setWidget(2, 1, permissionsFormWidgets.get(5));
		widgetList.setWidget(3, 0, permissionsFormWidgets.get(6));
		widgetList.setWidget(3, 1, permissionsFormWidgets.get(7));
		widgetList.setWidget(4, 0, permissionsFormWidgets.get(8));
		
		widgetList.getFlexCellFormatter().setColSpan(4, 0, 2);
		
		form.setWidget(0, 0, widgetList);
		
	}
	
	private void generateRolesPermissionsForm(){
		widgetList = new FlexTable();
		rolePermissionsFormWidgets = new ArrayList<Widget>();
		
		rolePermissionsFormWidgets.add(new ListBox());
		rolePermissionsFormWidgets.add(new FlexTable());
		rolePermissionsFormWidgets.add(new TextBox());
		rolePermissionsFormWidgets.add(new ListBox());
		rolePermissionsFormWidgets.add(new Button("Add"));
		
		HorizontalPanel addRole = new HorizontalPanel();
		addRole.add(new Label("Role name: "));
		addRole.add(rolePermissionsFormWidgets.get(2));
		addRole.add(new Label(" copy from: "));
		addRole.add(rolePermissionsFormWidgets.get(3));
		addRole.add(rolePermissionsFormWidgets.get(4));
		
		widgetList.getFlexCellFormatter().setColSpan(0, 0, 2);
		widgetList.setWidget(0, 0, addRole);
		widgetList.setWidget(1, 0, new Label("Role:"));
		widgetList.setWidget(1, 1, rolePermissionsFormWidgets.get(0));
		widgetList.getFlexCellFormatter().setColSpan(2, 0, 2);
		widgetList.setWidget(2, 0, rolePermissionsFormWidgets.get(1));
		
		form.setWidget(0, 0, widgetList);
	}
	
	public void generateLanguageForm(){
		
	}
	
	public void generateAppSettingsForm(){
		widgetList = new FlexTable();
		appSettingsFormWidgets = new ArrayList<Widget>();
		
		//general
		appSettingsFormWidgets.add(new TextBox());//title
		appSettingsFormWidgets.add(new TextBox());//logo
		//data base
		appSettingsFormWidgets.add(new TextBox());//hostname
		appSettingsFormWidgets.add(new TextBox());//port
		appSettingsFormWidgets.add(new TextBox());//username
		appSettingsFormWidgets.add(new TextBox());//password
		appSettingsFormWidgets.add(new TextBox());//name
		//menu
		appSettingsFormWidgets.add(new TextArea());//menu
		appSettingsFormWidgets.get(7).setHeight("150px");
		appSettingsFormWidgets.get(7).setWidth("300px");
		
		widgetList.getFlexCellFormatter().setColSpan(0, 0, 2);
		widgetList.setWidget(0, 0, new Label("Application options"));
		widgetList.setWidget(1, 0, new Label("Title:"));
		widgetList.setWidget(1, 1, appSettingsFormWidgets.get(0));
		widgetList.setWidget(2, 0, new Label("Logo:"));
		widgetList.setWidget(2, 1, appSettingsFormWidgets.get(1));
		widgetList.getFlexCellFormatter().setColSpan(3, 0, 2);
		widgetList.setWidget(3, 0, new Label("Data Base options"));
		widgetList.setWidget(4, 0, new Label("Host name:"));
		widgetList.setWidget(4, 1, appSettingsFormWidgets.get(2));
		widgetList.setWidget(5, 0, new Label("Port:"));
		widgetList.setWidget(5, 1, appSettingsFormWidgets.get(3));
		widgetList.setWidget(6, 0, new Label("Username:"));
		widgetList.setWidget(6, 1, appSettingsFormWidgets.get(4));
		widgetList.setWidget(7, 0, new Label("Password:"));
		widgetList.setWidget(7, 1, appSettingsFormWidgets.get(5));
		widgetList.setWidget(8, 0, new Label("Data Base Name:"));
		widgetList.setWidget(8, 1, appSettingsFormWidgets.get(6));
		widgetList.getFlexCellFormatter().setColSpan(9, 0, 2);
		widgetList.setWidget(9, 0, new Label("Menu structure"));
		widgetList.getFlexCellFormatter().setColSpan(10, 0, 2);
		widgetList.setWidget(10, 0, appSettingsFormWidgets.get(7));
		
		widgetList.getWidget(0, 0).setStyleName("form_section");
		widgetList.getWidget(3, 0).setStyleName("form_section");
		widgetList.getWidget(9, 0).setStyleName("form_section");
		
		form.setWidget(0, 0, widgetList);
	}
	
	public ArrayList<Widget> getPageWidgets(){
		return mainFormWidgets;
	}
	
	public ArrayList<Widget> getEventWidgets(){
		return eventFormWidgets;
	}
	
	public ArrayList<Widget> getForeignWidgets(){
		return foreignFormWidgets;
	}
	
	public ArrayList<Widget> getUserWidgets(){
		return userFormWidgets; 
	}
	
	public ArrayList<Widget> getPermissionsWidgets(){
		return permissionsFormWidgets;
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}

	public Button getCreateButton(){
		return create;
	}
	
	public Button getCancelButton(){
		return cancel;
	}
	
	public WindowPanel getModalWindow(){
		return winModal;
	}
	
	public TextBox getNameTextBox(){
		return name;
	}
	
	public TextBox getPageNameTextBox(){
		return pageName;
	}
	
	public Button getAddPageButton(){
		return addPage;
	}
	
	public Button getAddEventButton(){
		return addEvent;
	}
	
	public Button getAddForeignButton(){
		return addForeign;
	}
	
	public FlexTable getObjectList(){
		return objectList;
	}
	
	public void setObject(String name){
		objectList.setWidget(currentIndex, 0, new Label(name));
		currentIndex++;
	}
	
	public Button getPageNameButton(){
		return setPageName;
	}
	
	public Button getSaveButton(){
		return save;
	}
	
	public Button getAddUserButton(){
		return addUser;
	}
	
	public Button getAddPermissionButton(){
		return addPermission;
	}

	@Override
	public ArrayList<Widget> getRolesPermissionsWidgets() {
		return rolePermissionsFormWidgets;
	}

	@Override
	public ArrayList<Widget> getAppSettingsWidgets() {
		return appSettingsFormWidgets;
	}

	@Override
	public Button getAppSettingsButton() {
		return saveAppSettings;
	}

}
